<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" v-model="bindTaskName" />
    <a-button type="primary" @click="addTask">添加事项</a-button>

    <a-list bordered :dataSource="$store.getters.activeList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox
          :class="{ done: item.done }"
          @change="changeStatus(item.id, $event)"
          :checked="item.done"
          >{{ item.info }}</a-checkbox
        >
        <!-- 删除链接 -->
        <a slot="actions" @click="delTask(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>未完成: {{ $store.getters.doneLength }}</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            :type="activeListName === 'all' ? 'primary' : ''"
            @click="changeList('all')"
            >全部</a-button
          >
          <a-button
            :type="activeListName === 'undone' ? 'primary' : ''"
            @click="changeList('undone')"
            >未完成</a-button
          >
          <a-button
            :type="activeListName === 'done' ? 'primary' : ''"
            @click="changeList('done')"
            >已完成</a-button
          >
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'App',
  created() {
    const list = [
      {
        id: 0,
        info: 'Racing car sprays burning fuel into crowd.',
        done: false
      },
      { id: 1, info: 'Japanese princess to wed commoner.', done: false },
      {
        id: 2,
        info: 'Australian walks 100km after outback crash.',
        done: false
      },
      { id: 3, info: 'Man charged over missing wedding girl.', done: false },
      { id: 4, info: 'Los Angeles battles huge wildfires.', done: false }
    ]
    this.$store.commit(
      'initList',
      JSON.parse(window.localStorage.getItem('listData')) || list
    )
  },
  methods: {
    // 添加事项
    addTask() {
      if (this.taskName === '') {
        return this.$message.warning('请输入内容!')
      }
      if (this.list.some(v => v.info === this.taskName)) {
        return this.$message.warning('事项已存在!!')
      }
      this.$store.commit('addTask')
    },
    // 删除事项
    delTask(id) {
      const result = confirm('你确定要删除吗?')
      if (result) {
        this.$store.commit('delTask', id)
      }
    },
    // 改变状态
    changeStatus(id, e) {
      this.$store.commit('changeStatus', { id: id, done: e.target.checked })
    },
    // 删除全部
    clear() {
      this.$store.commit('clear')
    },
    // 切换全部未完成完成
    changeList(activeList) {
      this.$store.commit('changeList', activeList)
    }
  },
  computed: {
    ...mapState(['taskName', 'list', 'activeListName']),
    // 通过计算属性实现文本框与vuex中的数据进行绑定
    bindTaskName: {
      get() {
        return this.$store.state.newTaskName
      },
      set(name) {
        this.$store.commit('setNewTaskName', name)
      }
    }
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.done {
  color: #eee;
  text-decoration: line-through;
}
</style>
